package com.zkx.blogweb.mapper.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zkx.blogweb.entity.IdArticle;
import com.zkx.blogweb.entity.User;
import com.zkx.blogweb.mapper.IdArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zkx
 */
@Transactional
@Service
public class IdArticleServiceImp implements IdArticleService{


    @Autowired
    IdArticleMapper idArticleMapper;


    /**
     * 根据id
     */
    public IdArticle getArticleByBlogId(Integer blogId){
        return idArticleMapper.selectByPrimaryKey(blogId);
    }


    /**
     * 得到个人的文章总数返回分页给前端
     * @param userId
     * @param startPageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<IdArticle> getPageInfoByBlogId(Integer userId, Integer startPageNum, Integer pageSize) {
        PageHelper.startPage(startPageNum,pageSize);
        //得到文章page
        List<IdArticle> pageIdArticleList = getListByBlogId(userId,startPageNum,pageSize);
        PageInfo<IdArticle> PageInfos = new PageInfo<>(pageIdArticleList);
        return PageInfos;
    }

    /**
     * 得到个人的文章总数
     * @param userId
     * @param start
     * @param pageSize
     * @return
     */
    @Override
    public List<IdArticle> getListByBlogId(Integer userId,Integer start,Integer pageSize) {
        return idArticleMapper.getListByBlogId(userId,start,pageSize);
    }

    @Override
    public PageInfo<IdArticle> getPageInfo(Integer startPageNum, Integer pageSize) {
        PageHelper.startPage(startPageNum,pageSize);
        //得到文章page
        List<IdArticle> pageIdArticleList = getPageIdArticleList(startPageNum,pageSize);
        PageInfo<IdArticle> PageInfos = new PageInfo<>(pageIdArticleList);
        return PageInfos;
    }

    @Override
    public List<IdArticle> getPageIdArticleList(Integer start,Integer pageSize) {
        return idArticleMapper.getPageIdArticleList(start,pageSize);
    }

    /**
     * 将查询的数据转封成Map< >
     * @return
     */
    @Override
    public HashMap<String,String> getAllBlogUserName() {
        List<HashMap<Integer, String>> allBlogUserName =
                idArticleMapper.getAllBlogUserName();
        HashMap<String, String> resMap = new HashMap<>();
        for(HashMap<Integer, String> map : allBlogUserName){
            String key = "";
            String value = "";
            for(Map.Entry<Integer, String> entry : map.entrySet()){
                if ("user_id".equals(entry.getKey())) {
                    key =  String.valueOf(entry.getValue());
                } else if ("user_name".equals(entry.getKey()+"")){
                    value = entry.getValue();
                }
                resMap.put(key,value);
            }
        }

        return resMap;
    }

    @Override
    public int allArticles() {
        return idArticleMapper.allArticles();
    }

    @Override
    public Integer selectBlogsById(Integer userId) {
        return idArticleMapper.selectBlogsById(userId);
    }


    public Integer selectStarsById(Integer userId) {
        return idArticleMapper.selectStarsById( userId);
    }

    @Override
    public void deleteBlogById(int blogId) {
        idArticleMapper.deleteByPrimaryKey(blogId);
    }

    @Override
    public void deleteBlogByUserId(Integer userId) {
        idArticleMapper.deleteBlogByUserId(userId);
    }

    @Override
    public List<IdArticle> getRankTop() {
        return idArticleMapper.getRankTop();
    }

    @Override
    public void insertIdArtilce(IdArticle idArticle) {
            idArticleMapper.insertSelective(idArticle);
    }

    @Override
    public IdArticle selectUserByBlogId(Integer searchBlogId) {
        return idArticleMapper.selectByPrimaryKey(searchBlogId);
    }


}
